package com.visikard.request;

import java.net.ConnectException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.visikard.application.APIURL;
import com.visikard.net.VKregisterException;
import com.visikard.net.VKregisterNetworkException;
import com.visikard.net.VkRequestMethod;
import com.visikard.net.VkRestClient;
import com.visikard.system.VKregisterConnects;
import com.visikard.utility.HttpsUtils;

public class TransactionHistoryRequest extends BaseRequest {

	/**
	 * constructor
	 */
	public TransactionHistoryRequest() {
		super();
	}

	// get gift kards transaction history by date
	public String getTransactionHistoryByDateNew(long uid, String isStaff, int txType, String sDate, String eDate,
			String timezone, int pageIndex, int pageSize) throws VKregisterException {

		VkRestClient rc = new VkRestClient(VKregisterConnects.getApiContextPathHttp()
				+ APIURL.GET_REDEEMED_HISTORY_NEW_DATE);

		rc.addParam("userId", "" + uid);
		rc.addParam("isStaff", "" + isStaff);
		rc.addParam("txType", txType);
		rc.addParam("sDate", sDate);
		rc.addParam("eDate", eDate);
		rc.addParam("timezone", timezone);
		rc.addParam("pageIndex", "" + pageIndex);
		rc.addParam("pageSize", "" + pageSize);

		rc.execute(VkRequestMethod.GET);
		if (rc.getResponseCode() != HttpStatus.SC_OK) {
			return rc.getErrorMessage();
		}

		return rc.getResponse();

	}

	// get gift kards transaction history by date
	public String getTransactionHistoryGiftKardByDate(long uid, String isStaff, String sDate, String eDate,
			int pageIndex, int pageSize) throws VKregisterException {

		VkRestClient rc = new VkRestClient(VKregisterConnects.getApiContextPathHttp()
				+ APIURL.GET_GIFT_KARDS_REDEEMED_HISTORY_DATE);

		rc.addParam("userId", "" + uid);
		rc.addParam("isStaff", "" + isStaff);
		rc.addParam("sDate", sDate);
		rc.addParam("eDate", eDate);
		rc.addParam("pageIndex", "" + pageIndex);
		rc.addParam("pageSize", "" + pageSize);

		rc.execute(VkRequestMethod.GET);
		if (rc.getResponseCode() != HttpStatus.SC_OK) {
			return rc.getErrorMessage();
		}

		return rc.getResponse();

	}

	// get coupon kards transaction history by date
	public String getTransactionHistoryCouponKardByDate(long uid, String isStaff, String sDate, String eDate,
			int pageIndex, int pageSize) throws VKregisterException {

		VkRestClient rc = new VkRestClient(VKregisterConnects.getApiContextPathHttp()
				+ APIURL.GET_COUPON_KARDS_REDEEMED_HISTORY_DATE);

		rc.addParam("userId", "" + uid);
		rc.addParam("isStaff", "" + isStaff);
		rc.addParam("sDate", sDate);
		rc.addParam("eDate", eDate);
		rc.addParam("pageIndex", "" + pageIndex);
		rc.addParam("pageSize", "" + pageSize);

		rc.execute(VkRequestMethod.GET);
		if (rc.getResponseCode() != HttpStatus.SC_OK) {
			return rc.getErrorMessage();
		}

		return rc.getResponse();
	}

	// get punch kards transaction history: New API
	public String getTransactionHistoryPunchKard(long uid, String isStaff, int pageIndex, int pageSize)
			throws VKregisterException {

		VkRestClient rc = new VkRestClient(VKregisterConnects.getApiContextPathHttp()
				+ APIURL.GET_PUNCH_KARDS_REDEEMED_HISTORY_DATE);

		rc.addParam("userId", "" + uid);
		rc.addParam("isStaff", "" + isStaff);
		rc.addParam("sDate", "");
		rc.addParam("eDate", "");
		rc.addParam("pageIndex", "" + pageIndex);
		rc.addParam("pageSize", "" + pageSize);

		rc.execute(VkRequestMethod.GET);
		if (rc.getResponseCode() != HttpStatus.SC_OK) {
			return rc.getErrorMessage();
		}

		return rc.getResponse();
	}

	public String getTransactionHistoryRewardedPunchKard(long uid, int pageIndex, int pageSize)
			throws VKregisterException {

		HttpClient httpClient = HttpsUtils.getInstance().sslClient(new DefaultHttpClient());
		HttpPost httppost = new HttpPost(VKregisterConnects.getApiContextPathHttp()
				+ APIURL.GET_PUNCH_KARDS_REWARDED_HISTORY);
		httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("userId", "" + uid));
		nameValuePairs.add(new BasicNameValuePair("sDate", ""));
		nameValuePairs.add(new BasicNameValuePair("eDate", ""));
		nameValuePairs.add(new BasicNameValuePair("pageIndex", "" + pageIndex));
		nameValuePairs.add(new BasicNameValuePair("pageSize", "" + pageSize));

		try {
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
			HttpResponse response = httpClient.execute(httppost);
			return EntityUtils.toString(response.getEntity()).trim();
		}
		catch (ConnectException e) {
			throw new VKregisterNetworkException(e.getMessage());
		}
		catch (Exception e) {
			throw new VKregisterException(e.getMessage());
		}
	}

}